﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using DevComponents.DotNetBar;
namespace QLST
{
    public partial class frmNhanVien : Form
    {
        NhanVienBUS cls=new NhanVienBUS() ;
        NhanVienDTO p=new NhanVienDTO ();
        bool flag;
        public frmNhanVien()
        {
            InitializeComponent();
        }

        private void frmNhanVien_Load(object sender, EventArgs e)
        {
            ClearTextBox();
            LockTextBox(false);
            LockButton(true);
            LoadNV();
        }
        public void LoadNV()
        {
            gvNhanVien.DataSource =cls.Load_NhanVien();
        }

        private void btnNVThem_Click(object sender, EventArgs e)
        {
            LockButton(false);
            LockTextBox(true);
            ClearTextBox();
            gvNhanVien.Enabled = false;
            txtHoTen.Focus();
        }

        private void btnNVXoa_Click(object sender, EventArgs e)
        {
            p.MaNV = txtMaNV.Text;
            cls.Del_NhanVien(p);
            frmNhanVien_Load(sender, e);
        }

        private void btnNVSua_Click(object sender, EventArgs e)
        {
            LockTextBox(true);
            LockButton(false);
            gvNhanVien.Enabled = false;
            txtHoTen.Focus();   
        }

        private void btnNVLuu_Click(object sender, EventArgs e)
        {
            if(txtMaNV .Text =="")
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            p.DiaChi = txtDiaChi.Text;
            p.DienThoai = txtDienThoai.Text;
            p.MaNV = txtMaNV.Text;
            p.NgaySinh = dateNgaySinh.Value;
            p.TenNV = txtHoTen.Text;

            cls.IsUp_NhanVien(p, flag);
            ClearGV();
            frmNhanVien_Load(sender, e);
        }
        //
        private void gvNhanVien_SelectionChanged(object sender, EventArgs e)
        {
            txtMaNV.Text = gvNhanVien.CurrentRow.Cells["colMaNV"].Value.ToString();
            txtHoTen.Text = gvNhanVien.CurrentRow.Cells["colHoTen"].Value.ToString();
            txtDiaChi.Text = gvNhanVien.CurrentRow.Cells["colDiaChi"].Value.ToString();
            txtDienThoai.Text = gvNhanVien.CurrentRow.Cells["colDienThoai"].Value.ToString();
            dateNgaySinh.Text = gvNhanVien.CurrentRow.Cells["colNgaySinh"].Value.ToString();

        }
        //
        private void btnNVHuy_Click(object sender, EventArgs e)
        {
            ClearGV();
            txtTimKiem.Text = "";
            frmNhanVien_Load(sender, e);
            LockButton(true);
            LockTextBox(false);
            gvNhanVien.Enabled = true;
        }

        private void btnNVTim_Click(object sender, EventArgs e)
        {
            p.MaNV = txtTimKiem.Text;
            p.TenNV  = txtTimKiem.Text;
            if (cbMaNV.Checked == true)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            gvNhanVien.ClearSelection();
            try
            {
                gvNhanVien.DataSource = cls.TimNV(p, flag);
            }
            catch
            {
                MessageBoxEx.Show("Bạn nhập sai Mã Nhân Viên hoặc Tên Nhân Viên"," ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }                   
        }
        private void ClearGV()
        {
            gvNhanVien.ClearSelection();
        }

        private void ClearTextBox()
        {
            txtMaNV.Text = "";
            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            dateNgaySinh.Text = "";
        }

        private void LockButton(Boolean f)
        {
            btnNVThem.Enabled = f;
            btnNVSua.Enabled = f;
            btnNVXoa.Enabled = f;
            btnNVLuu.Enabled = !f;
            btnNVHuy.Enabled = !f;
        }

        private void LockTextBox(Boolean f)
        {
            txtMaNV.Enabled = f;
            txtHoTen.Enabled = f;
            txtDiaChi.Enabled = f;
            txtDienThoai.Enabled = f;
            dateNgaySinh.Enabled = f;

            if (f == false)
            {
                txtMaNV.BackColor = Color.LemonChiffon;
                txtHoTen.BackColor = Color.LemonChiffon;
                txtDiaChi.BackColor = Color.LemonChiffon;
                txtDienThoai.BackColor = Color.LemonChiffon;
                dateNgaySinh.BackColor = Color.LemonChiffon;
            }
            else if (f == true)
            {
                txtMaNV.BackColor = Color.White;
                txtHoTen.BackColor = Color.White;
                txtDiaChi.BackColor = Color.White;
                txtDienThoai.BackColor = Color.White;
                dateNgaySinh.BackColor = Color.White;
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            frmNhanVien_Load(sender, e);
        }

        

        
    }
}
